package com.hc.TestDrill;

import com.hc.R;
import com.hc.R.layout;
import com.hc.R.menu;
import com.hc.activity.MainActivity;
import com.hc.activity.util.ActivityApplicationUtil;
import com.hc.activity.util.DialogFactory;
import com.hc.commonData.SysCommonData;
import com.hc.model.EquipmentBean;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
/**
 * 
 * 训练模式界面
 * @author Administrator
 *
 */
public class TestDrillActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置没有title
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_test_drill);
		// 当前的activity加入到关闭集合中
		ActivityApplicationUtil.getInstance().addActivity(this);
		
		Button closeTestDrillButton = (Button) findViewById(R.id.closeTestDrillBtn);
		closeTestDrillButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				closeDrill();//调用结束训练方法
			}

		

			
		});
		
	}
	/**
	 * 
	 * 结束校准方法
	 */
	private void closeDrill() {

		
	TestDrillService.closeTestDrill(TestDrillActivity.this);
	final String result = 	TestDrillService.getStepKey(TestDrillActivity.this);
	
		if (result == null) 
		{
			
			Toast.makeText(TestDrillActivity.this.getApplicationContext(), "提示：获取步测key值失败，请重新训练", 1).show();
		} else {
			
			float f = Float.intBitsToFloat(new Integer(result));
			
			if(f==0){
				
				new AlertDialog.Builder(TestDrillActivity.this).setTitle("提示").setMessage("key值为O，请重新训练").setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(TestDrillActivity.this,MainActivity.class);
						startActivity(intent);
					}
				}).create().show();
				
				
			} else {
				
				resultHanlder(f+"");
			}
			
			
		
		}
	
		
	}
/**
 * 训练据结果处理方法
 * @param result
 */
	private void resultHanlder(final String result) {
		

		

		new AlertDialog.Builder(this).setTitle("提示").setMessage("设置的行走步长是"+SysCommonData.drillWalkLength+"米 \n 测试返回key值为 "+result)
		.setPositiveButton("保存并使用", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				TestDrillService.saveSetpKey(TestDrillActivity.this, result);
			}
		}).setNegativeButton("取消", new  DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(TestDrillActivity.this, MainActivity.class);
				startActivity(intent);//条状界面
				
			}
			
			
		}).setNeutralButton("仅使用",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				SysCommonData.stepKey = (float) (SysCommonData.drillWalkLength/(Float.parseFloat(result)));
				
				Intent intent = new Intent(TestDrillActivity.this, MainActivity.class);
				startActivity(intent);//条状界面
			}
		}).create().show();
		
	
	
		
	}
	
	// 对返回按钮进行监听
	@Override
	public void onBackPressed() {
		new AlertDialog.Builder(this).setTitle("提示").setMessage("退出会结束训练 ， 是否退出训练")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						closeDrill();
					}
				}).setNegativeButton("取消", null).create().show();
	}
}
